//
//  FLLShikeViewController.m
//  FLLGodOfCookery
//
//  Created by LL.F on 16/8/27.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLShikeViewController.h"
#import "FLLShikeTableViewCell.h"
#import "ZWYNetTool.h"
#import "FLLShikeModel.h"
#import "ShikeData.h"
#import "ShikeSecData.h"
#import "FLLMacro.h"
#import "UIImageView+WebCache.h"
#import "FLLShikeHeaderView.h"
#import "FLLShikeHeaderCollectionViewCell.h"
#import "FLLShikeAlbumModel.h"
#import "Albums.h"
#import "AlbumData.h"
#import "FLLDetailViewController.h"
@interface FLLShikeViewController ()<UITableViewDataSource, UITableViewDelegate,UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UITableView *shikeTableView;
@property (nonatomic, strong) FLLShikeModel *fllShikeModel;
@property (nonatomic, strong) NSMutableArray<ShikeSecData *> *tableArray;
@property (nonatomic, strong) FLLShikeAlbumModel *fllAlbumsModel;
@property (nonatomic, strong) NSArray<Albums *> *albumsArray;
@property (nonatomic, strong) FLLShikeHeaderView *headerView;
@property (nonatomic, assign) BOOL isLoading;
@property (nonatomic, assign) NSInteger pageCount;
@end

@implementation FLLShikeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createPOSTRequest];
    [self createTableViewRequest];
    [self createShikeTableView];
}
- (void)createPOSTRequest
{
    // 区头视图
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1" andBody:@"--3eeab6bd-fd17-4446-a259-abd68e5dbe15\nContent-Disposition: form-data; name=\"methodName\"\nContent-Length: 10\n\nCourseLogo\n--3eeab6bd-fd17-4446-a259-abd68e5dbe15" andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"multipart/form-data; boundary=3eeab6bd-fd17-4446-a259-abd68e5dbe15"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        self.fllAlbumsModel = [FLLShikeAlbumModel modelWithDic:result];
        self.albumsArray = self.fllAlbumsModel.data.albums;
        [_headerView.albumCollectionView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        
    }];
    
    
}
- (void)createTableViewRequest
{

    // 下面tableView
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1" andBody:[NSString stringWithFormat: @"page=%zd&methodName=CourseIndex&size=20&version=4.40&", _pageCount]andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        if (_isLoading == NO) {
            _pageCount = 1;
        } else {
        
            _pageCount++;
        }
        self.fllShikeModel = [FLLShikeModel modelWithDic:result];
        if (_isLoading == NO) {
            self.tableArray = (NSMutableArray *)self.fllShikeModel.data.data;
        } else {
        
            [self.tableArray addObjectsFromArray:(NSMutableArray *)self.fllShikeModel.data.data];
        }
        
        [self.shikeTableView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        
    }];
    

    
}
- (void)createShikeTableView
{

    self.shikeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 64)  style:UITableViewStylePlain];
    [self.view addSubview:_shikeTableView];
    _shikeTableView.delegate = self;
    _shikeTableView.dataSource = self;
    _shikeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_shikeTableView registerNib:[UINib nibWithNibName:@"FLLShikeTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"shikeTableCell"];
    [_shikeTableView registerClass:[FLLShikeHeaderView class] forHeaderFooterViewReuseIdentifier:@"shikeHeaderView"];
    
    self.shikeTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _isLoading = YES;
        [self createTableViewRequest];
        [self.shikeTableView.mj_footer endRefreshing];
    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return _tableArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    FLLShikeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"shikeTableCell"];
    cell.grayLabel.backgroundColor = FLLLightGrayColor;
    [cell.dishImageView sd_setImageWithURL:[NSURL URLWithString:_tableArray[indexPath.row].image] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    
    NSString *contentString = _tableArray[indexPath.row].series_name;
    NSArray *subArray = [contentString componentsSeparatedByString:@"#"];
    NSString *subString = subArray.lastObject;
    cell.contentLabel.text = subString;
    cell.contentLabel.font = [UIFont systemFontOfSize:16 * kWidthScale];
    cell.updateCountLabel.text = [NSString stringWithFormat:@"更新至%ld集",(long) _tableArray[indexPath.row].episode];
    cell.updateCountLabel.font = [UIFont systemFontOfSize:14 * kWidthScale];
    
    cell.inClassCountLabel.text = [NSString stringWithFormat:@"上课人数%ld", (long)_tableArray[indexPath.row].play];
    cell.inClassCountLabel.font = [UIFont systemFontOfSize:14 * kWidthScale];
    
    cell.topicLabel.text = _tableArray[indexPath.row].album;
    cell.topicLabel.font = [UIFont systemFontOfSize:14 * kWidthScale];
    
    [cell.topicImageView sd_setImageWithURL:[NSURL URLWithString:_tableArray[indexPath.row].album_logo] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    
    return cell;
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    self.headerView = [[FLLShikeHeaderView alloc] initWithReuseIdentifier:@"shikeHeaderView"];
    [_headerView.albumCollectionView registerNib:[UINib nibWithNibName:@"FLLShikeHeaderCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"shikeHeaderCollectionCell"];
    _headerView.albumCollectionView.delegate = self;
    _headerView.albumCollectionView.dataSource = self;
    _headerView.albumCollectionView.showsHorizontalScrollIndicator = NO;
    _headerView.albumCollectionView.backgroundColor = [UIColor clearColor];

    return _headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return _shikeTableView.bounds.size.height * 0.47 * kHeightScale;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    return SCREEN_HEIGHT * 0.15;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


    FLLDetailViewController *detailVC = [[FLLDetailViewController alloc] init];
    detailVC.passString = [NSString stringWithFormat:@"%zd", _tableArray[indexPath.row].series_id];
    [self.navigationController pushViewController:detailVC animated:YES];
}
#pragma mark -- CollectionView DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return _albumsArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    FLLShikeHeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"shikeHeaderCollectionCell" forIndexPath:indexPath];
    [cell.albumImageView sd_setImageWithURL:[NSURL URLWithString:_albumsArray[indexPath.row].album_logo] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    return CGSizeMake(collectionView.bounds.size.width / 4.5, collectionView.bounds.size.height);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *idString = self.albumsArray[indexPath.row].series_id;
    FLLDetailViewController *detailVC = [[FLLDetailViewController alloc] init];
    detailVC.passString = idString;
    [self.navigationController pushViewController:detailVC animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
